﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;

namespace InformaticaDiary.WebServiceConsumer
{
    class CallWebService
    {
        public string CallServicePOST(string ServiceURl, string methodname, string jsonContent)
        {
            string userName = "";// ConfigHelper.SLDecrypt(ConfigHelper.GetAppSetting("JavaWebServiceUserName"));
            string password = "";//ConfigHelper.SLDecrypt(ConfigHelper.GetAppSetting("JavaWebServicePassword"));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ServiceURl + methodname);
            //request.Credentials = new NetworkCredential(userName, password);// CredentialCache.DefaultNetworkCredentials;
            request.Timeout = Timeout.Infinite;
            request.KeepAlive = true;
            request.Method = "POST";
            Byte[] byteArray = Encoding.UTF8.GetBytes(jsonContent);
            request.ContentLength = byteArray.Length;
            request.ContentType = @"application/json";
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            string responseJson = "";
            try
            {
                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                }
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (var streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        responseJson = streamReader.ReadToEnd();
                    }
                    response.Close();
                }
            }
            catch (WebException ex)
            {
                //ExceptionLogger LogError("Json Error URl: " + ServiceURl + ", Method Name : " + methodname + ", jsonContent : " + jsonContent, null, ex);
                throw ex;
            }
            catch (Exception ex)
            {
                // Log exception and throw as for GET example above 
                //LogError("Json Error URl: " + ServiceURl + ", Method Name : " + methodname + ", jsonContent : " + jsonContent, null, ex);
                throw ex;


            } return responseJson;
        }
        public string CallServiceGet(string ServiceURl, string methodname, string jsonContent)
        {
            string userName = "";// ConfigHelper.SLDecrypt(ConfigHelper.GetAppSetting("JavaWebServiceUserName"));
            string password = "";//ConfigHelper.SLDecrypt(ConfigHelper.GetAppSetting("JavaWebServicePassword"));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ServiceURl + methodname);
            //request.Credentials = new NetworkCredential(userName, password);// CredentialCache.DefaultNetworkCredentials;
            request.Timeout = Timeout.Infinite;
            request.KeepAlive = true;
            request.Method = "GET";
            Byte[] byteArray = Encoding.UTF8.GetBytes(jsonContent);
            request.ContentLength = byteArray.Length;
            request.ContentType = @"application/json";
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            string responseJson = "";
            try
            {
                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                }
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (var streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        responseJson = streamReader.ReadToEnd();
                    }
                    response.Close();
                }
            }
            catch (WebException ex)
            {
                //ExceptionLogger LogError("Json Error URl: " + ServiceURl + ", Method Name : " + methodname + ", jsonContent : " + jsonContent, null, ex);
                throw ex;
            }
            catch (Exception ex)
            {
                // Log exception and throw as for GET example above 
                //LogError("Json Error URl: " + ServiceURl + ", Method Name : " + methodname + ", jsonContent : " + jsonContent, null, ex);
                throw ex;


            } return responseJson;
        }
    }
}
